package com.tomcat.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//import com.tomcat.hbase.MyHbase;
//
//import edu.whu.rose.clock.ssql.sang.search.SimpleQueryProcessingManager;

public class MoreInfo extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

//	MyHbase myHbase=null;
//	SimpleQueryProcessingManager simpleQuery = null;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String id = req.getParameter("id");
		Date start = new Date();
//		int[] answers = simpleQuery.getNeighborsFromGraphMatrix(Integer.parseInt(id));
//		String[][] info = createMoreInfo(answers,id);
		Date end = new Date();
//        req.setAttribute("moreResult", info);
//        req.setAttribute("orderYear", orderByYear(info));
        req.setAttribute("id", id);
        req.setAttribute("time",(end.getTime()-start.getTime())*1.0/1000+"s");
        req.getRequestDispatcher("more.jsp").forward(req,resp);	
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}

	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
//		myHbase = MyHbase.getInstance();
//		simpleQuery = SimpleQueryProcessingManager.getInstance();
//		if(simpleQuery.isGraphNull()){
//			simpleQuery.init(this.getServletContext().getRealPath("graph/edge_big.txt"),198462);
//		}
	} 
	
	public String[][]  createMoreInfo(int[] a,String id){
				
		String rel ="";
		String[][] result = null;
		if(a != null){		
			int[][] aRel = new int[a.length-1][3];//except itself
			
			for(int i=0,m=0;i<a.length;i++){
				if(a[i] != Integer.parseInt(id)){
					
//					rel = myHbase.getEdgeInfo(id+"-->"+a[i]);
					if(rel==""){
//						rel = myHbase.getEdgeInfo(a[i]+"-->"+id);
						if(rel!=""){
							aRel[m][2] = -1;// <--
						}
					}else{
						aRel[m][2] = 1;//-->
					}
					
					aRel[m][0]=a[i];
					
					if(rel.equals("write")) {
						aRel[m][1] = 1;
					}else if(rel.equals( "edit")){
						aRel[m][1] = 2;
					}else if(rel.equals("cite")){
						aRel[m][1] = 3;
					}else if(rel.equals("inproceedings")){
						aRel[m][1] = 4;
					}else if(rel.equals("publish")){
						aRel[m][1] = 5;
					}else{
						aRel[m][1] = 6;
					}
					
					m++;
				}				
			}
			
			int[] relNum=new int[6];	
			for(int i=0;i<aRel.length;i++){
				relNum[aRel[i][1]-1]++; //0-writeNum;1-editNum....5...
			}
			
			result = new String[6][];//0-w,1-edit...
			
			for(int i = 0;i<result.length;i++){
				result[i] = new String[relNum[i]+1];//initialize null
			}
			
			for(int i=0;i<aRel.length;i++){
				int ch = aRel[i][1]-1;//choice
//				result[ch][relNum[ch]] = aRel[i][0]+"@@"+myHbase.getInfoTest(aRel[i][0]);
				if((--relNum[ch]) == 0){
					result[ch][0] = aRel[i][2]+"";
				}
			}
		}
		return result;
		
//		for(int i=0;i<result.length;i++){
//			for(int j=0;j<result[i].length;j++){
//				String s = result[i][j];
//				String[] columns = s.split("@@");
//				for(String str:columns){
//					if(j == 0){//th
//						String th= str.split(":")[0];
//					}if(j == result[i].length -1){//more info
//						String more=" ?id="+id;
//					}else{//td
//						String td = str.split(":")[1];
//					}
//				}				
//			}
//		}
		
		
	}

	public int[][] orderByYear(String[][] res){
		int[][] result = new int[res.length][];
		int maxTitleNum=0;//number of the table titles
		int index=0;
		int size=0;
		int firstYearIndex=0 ,endYearIndex=0,year=0;
		for(int i =0;i<res.length;i++){
			result[i] = new int[res[i].length];
			if(res[i].length == 1){//no records
				continue;
			}
			for(int j=1;j<res[i].length;j++){				
				size = res[i][j].split("@@").length;
				if(size > maxTitleNum) {
					maxTitleNum = size;
					index = j;
				}
				
				firstYearIndex = res[i][j].indexOf("@@year");
				if(firstYearIndex == -1){
					result[i][j] = j; 
					continue;
				}
				endYearIndex = res[i][j].indexOf("@@", firstYearIndex+2);
				if(endYearIndex == -1){
					endYearIndex = res[i][j].length();
				}
				year = Integer.parseInt(res[i][j].substring(firstYearIndex, endYearIndex).split(":")[1]);
				result[i][j] = year;
				
			}//end inner for
			
			result[i][0] = index;//the longest title
			index=0;
			maxTitleNum=0;
			
			sortByChangeIndex(result[i],1,res[i].length-1);//from 1 to res[i].length-1
		}
		
		return result;
	}
	
	public void sortByChangeIndex(int[] rs,int start,int longth){
		int [] index = new int[longth -start+1];
		int temp=0;
		for(int i=0;i<index.length;i++){
			index[i] = i+1;
		}
		
		for(int i=0;i<index.length-1;i++){
			for(int j=0;j<index.length-i-1;j++){
				if(rs[index[j]] < rs[index[j+1]]){
					temp = index[j];index[j]=index[j+1];index[j+1]=temp;
				}
			}
		}
		for(int i=0;i<index.length;i++){
			rs[start+i] = index[i];
		}
	}
}
